<template>
  <!-- 战败车型 -->
  <div>
    <TopFilter :list="topFilterList" :cols="3" labelWidth="100" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable
      ref="table"
      height="auto"
      :columns="columns"
      :isSelectColumn="false"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visible" :title="dialogTitle" width="60%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <dealerBlacklistModels :type="handleType" :rowData="formData" @close="closeHandler" />
    </Drawer>
    <Drawer :visible.sync="reassign" :title="dialogTitle" width="60%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <reassignlistModels :type="handleType" :rowData="formData" @close="closeHandler" />
    </Drawer>

    <el-dialog :visible.sync="delVisible" title="删除" :type="handleType" :rowData="formData" width="30%" center>
      <span>确定要删除吗?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeVisible">取 消</el-button>
        <el-button type="primary" @click="deleteHandler">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { queryCleanTask, deleteCleanTask } from '@/api/potenCus/basedata/dealerBlacklist';
import { dictionary } from '@/utils/dictMixin';
import dealerBlacklistModel from './dealerBlacklistModel';
import dealerBlacklistModels from './dealerBlacklistModels';
import reassignlistModels from './reassignlistModels';
import { exportExcelFile } from '@/utils/index';
import { getAllUser } from '@/api/customerRepair/afterSaleVisit/resultEntry';
import { getAppId } from '@/assets/js/auth';//获取登录用户

export default {
  name: 'dealerBlacklist',
  mixins: [dictionary],
  components: {
    dealerBlacklistModel,
    dealerBlacklistModels,
    reassignlistModels
  },
  data() {
    return {
      topFilterList: this.createTopFilters(),
      fetchParams: { xhrAbort: true, appId: getAppId() },
      visible: false,
      reassign: false,
      delVisible: false,
      // params,
      dialogTitle: '',
      fetchApi: queryCleanTask,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf: { add: '新增', edit: '编辑', show: '显示' }
    };
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '任务代码',
          fieldName: 'taskNum',
          placeholder: '请填写'
        },
        {
          type: 'INPUT',
          label: '任务描述',
          fieldName: 'taskName',
          placeholder: '请填写',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'isActivity',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'RANGE_DATE',
          label: '任务开始日期',
          fieldName: 'taskStDate|taskStDateEnd',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '任务结束日期',
          fieldName: 'taskEnDate|taskEnDateEnd',
          placeholder: '请选择',
          itemList: []
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 140,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.delHandle(props.row)}>
                  删除
                </el-button>
                <el-button size="mini" type="text" onClick={() => this.addReassign(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '任务代码',
          dataIndex: 'taskNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '任务描述',
          dataIndex: 'taskName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '任务开始日期',
          dataIndex: 'taskStDate',
          valueFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '任务结束日期',
          dataIndex: 'taskEnDate',
          valueFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否启用',
          dataIndex: 'isActivity',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.createDictText(props.row.isActivity, '1004')}</span>;
          }
        },
        {
          title: '创建人',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          dictItems: [],
          filterType: 'input'
        },
        {
          title: '创建时间',
          dataIndex: 'createdAt',
          valueFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '变更人',
          dataIndex: 'updatedBy',
          sorter: true,
          filter: true,
          dictItems: [],
          filterType: 'input'
        },
        {
          title: '变更时间',
          dataIndex: 'updatedAt',
          valueFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    init() {
      this.fetchParams = Object.assign({ xhrAbort: false }, { appId: getAppId() });
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    addReassign(row) {
      this.formData = row;
      this.handleType = 'edit';
      this.showHandler();
    },
    delHandle(row) {
      console.log('删除', row);
      this.handleType = 'delete';
      this.formData = row;
      this.dealerCode = row.dealerCode;
      this.dialogTitle = `${this.conf[this.handleType]}`;
      this.delVisible = true;
    },
    showHandler() {
      let title = this.handleType == 'edit' ? '编辑' : '新增';
      this.dialogTitle = title + `强制清洗任务`;
      this.visible = true;
    },
    // 导出
    async exportHandle(row) {
      // console.log('选择行', this.selectedRows);
      let params = JSON.parse(JSON.stringify(this.fetchParams));

      this.handleType = 'export';
      await exportDealerBlacklist(params)
        .then(response => {
          // console.log(response,"response")
          console.log(response.data);
          exportExcelFile(response.data, '经销商黑名单');
        })
        .catch(err => {
          console.log(err);
        });
    },
    showReassign() {
      this.dialogTitle = `添加改派经销商`;
      this.reassign = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({ xhrAbort: false }, val, { appId: getAppId() });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      this.reassign = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async deleteHandler() {
      const res = await deleteCleanTask(this.formData.cleanTaskId);
      console.log('删除', res);

      // if (res.resultCode === 200) {
      this.closeVisible();
      this.fetchParams = { ...this.fetchParams };
      // }
    },
    closeVisible() {
      this.delVisible = false;
    },
    async getUserName() {
      let res = await getAllUser();
      this.columns.find(x => x.dataIndex == 'createdBy').dictItems = res.data.map(x => ({ text: x.employeeName, value: x.userId }));
      this.columns.find(x => x.dataIndex == 'updatedBy').dictItems = res.data.map(x => ({ text: x.employeeName, value: x.userId }));
    }
  },
  mounted() {
    // init();
  },
  created() {
    this.init()
  }
};
</script>

<style lang="less" scoped></style>
